<?php
/**
 * Created by IntelliJ IDEA.
 * User: fang.cai.liang@aliyun.com
 * Date: 2016/12/26
 * Time: 20:46
 */

namespace app\index\controller;

use think\Db;


/**
 * Class TestController
 * @package app\xs\controller
 */
class MongoController {

    /**
     * @var array
     */
    private $str = ['a', 'b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z','A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'];

    /**
     * @var array
     */
    private $sexs = ['男', '女', '两性', '其他'];

    /**
     * @return string
     */
    private function genName(){
        $length = mt_rand(3, 8);
        $username = '';
        for($i = 0; $i < $length; $i ++){
            $username = $username.($this->str[mt_rand(0, count($this->str)-1)]);
        }
        return $username;
    }

    /**
     * @return int
     */
    public function minsert2(){
        set_time_limit(0);
        $s = time();
        for($n = 0; $n < 20; $n ++){
            $dataList = $this->buildData();
            Db::name('user2w')->insertAll($dataList);
            unset($dataList);
        }
        unset($dataList);
        $e = time();
        return ($e - $s);
    }

    /**
     * @return array
     */
    private function buildData(){
        $dataList = array();
        for($i = 0; $i < 1000; $i ++){
            unset($data);
            $data['username'] = $this->genName();
            $data['age'] = mt_rand(1, 100);
            $data['sex'] = $this->sexs[mt_rand(0, count($this->sexs) - 1)];
            $dataList[$i] = $data;
        }
        return $dataList;
    }
}